package com.mes.entity.llm;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Title:Node
 * @Package com.mes.entity.llm
 * @author llm
 * @date 2019年5月31日
 * @version 1.0
 */
public class Node {
	
	private Long id;    //树的节点Id
	private Long fid;
	private String cname;   //节点名称
	private Long monthMoney;   //月金额
	private Long yearMoney;		//年金额
	private List<Node> nodes;    //子节点，可以用递归的方法读取
	
	public Node() { 
        this.nodes = new ArrayList<Node>();
    }
    public Node(Long id, Long fid) {
        this.id = id;
        this.fid = fid;
        this.nodes = new ArrayList<Node>();
    }
    /**
     * 生成一个节点
     * @param nodeId
     * @param pId
     * @param text
     * @param icon
     * @param href
     */
    public Node(Long id, Long fid, String cname, Long monthMoney, Long yearMoney) {
        super();
        this.id = id;
        this.fid = fid;
        this.cname = cname;
        this.monthMoney = monthMoney;
        this.yearMoney = yearMoney;
        this.nodes = new ArrayList<Node>();
    }

    public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getFid() {
		return fid;
	}
	public void setFid(Long fid) {
		this.fid = fid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public Long getMonthMoney() {
		return monthMoney;
	}
	public void setMonthMoney(Long monthMoney) {
		this.monthMoney = monthMoney;
	}
	public Long getYearMoney() {
		return yearMoney;
	}
	public void setYearMoney(Long yearMoney) {
		this.yearMoney = yearMoney;
	}
	public List<Node> getNodes() {
		return nodes;
	}
	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}
	public List<Node> getNode() {
		return node;
	}
	public void setNode(List<Node> node) {
		this.node = node;
	}
	
	/**
     * 生成一颗多叉树，根节点为root
     * @param Nodes 生成多叉树的节点集合
     * @return NodeTree
     */
    public Node createTree(List<Node> Nodes) {
        if (Nodes == null || Nodes.size() < 0)
            return null;
        Node root = new Node(0L, 0L);
        // 将所有节点添加到多叉树中
        for (Node node : Nodes) {
            if (node.getFid().equals(0L)) {
                // 向根添加一个节点
                root.getNodes().add(node);
            } else {
                addChild(root, node);
            }
        }
        return root;
    }

    /**
     * 向指定多叉树节点添加子节点
     * @param Node 多叉树节点
     * @param child 节点
     */
    public void addChild(Node Node, Node child) {
        for (Node item : Node.getNodes()) {
            if (item.getId().equals(child.getFid())) {
                // 找到对应的父亲
                item.getNodes().add(child);
                break;
            } else {
                if (item.getNodes() != null && item.getNodes().size() > 0) {
                    addChild(item, child);
                }
            }
        }
    }

    /**
     * 遍历多叉树
     * @param Node 多叉树节点
     * @return
     */
    public String iteratorTree(Node Node) {
        StringBuilder buffer = new StringBuilder();
        buffer.append("\n");
        if (Node != null) {
            for (Node index : Node.getNodes()) {
                buffer.append(index.getId() + ",");
                if (index.getNodes() != null && index.getNodes().size() > 0) {
                    buffer.append(iteratorTree(index));
                }
            }
        }
        buffer.append("\n");
        return buffer.toString();
    }
    
    
    /**
     * 遍历多叉树
     * @param Node 多叉树节点
     * @return
     */
    List<Node> node = new ArrayList<Node>();
    public Node zxPraseTree(Node Node) {
        if (Node != null) {
            for (Node index : Node.getNodes()) {
                node.add(index);
                if (index.getNodes() != null && index.getNodes().size() > 0) {
                    node.add(zxPraseTree(index));
                }
            }
        }
        //buffer.append("\n");
        Node n = new Node();
        n.setNodes(node);
        return n;
    }

    public static void main(String[] args) {
        List<Node> nodes = new ArrayList<Node>();
        nodes.add(new Node(1L, 0L));
        nodes.add(new Node(2L, 1L));
        nodes.add(new Node(3L, 1L));
        nodes.add(new Node(4L, 1L));
        nodes.add(new Node(5L, 4L));
        nodes.add(new Node(5L, 4L));
        nodes.add(new Node(6L, 1L));

        Node tree = new Node();
        Node mt = tree.createTree(nodes);
        System.out.println(tree.iteratorTree(mt));
        Node n = tree.zxPraseTree(mt);
        List<Node> list = n.getNodes();
        for(Node node : list) {
            System.out.println(node.getId());
        }
    }
}
